<style lang="less">
    @import "approvalAsset.less";
</style>
<template>
    <div class="approvalAsset" >
        <remind :approve-record="remindApproveRecord"  @afterSubmit="afterRemindSubmit" v-model="remindVisible" :is-true="isTrue"/>
        <Collapse v-model="values" style="padding: 15px">
            <Row>
                <div class="line">
                    <Button @click="closeThis" type="primary" icon="md-return-left" >返回</Button>
                    <Button type="primary" icon="md-image">
                        <DynamicLink component="projects/afs-core-business/pages/case/approve/task-detail/regular-approve/regularApproveDetail"
                         :is-full="true" :params="{isLung:'true',taskId:this.detailParams.stageId,applyNo:this.detailParams.applyNo}" name="案件详情" page-title="案件详情">
                        </DynamicLink>
                    </Button>
                    <Button  icon="md-camera" type="primary">
                        <DynamicLink component="projects/basic/pages/image/file-audit/createFileAudit"
                                     :is-full="true" :params="{auditParam:this.auditParam,operate:this.isCheck,businessDic:[{value:'assetsChange',title:'资产变更'}]}" name="影像资料" page-title="影像资料">
                        </DynamicLink>
                    </Button>
                    <Button @click="modifyRemind" type="primary"  icon="md-send" v-if="isCheck">添加留言</Button>
                    <Button @click="addPaper" type="primary" icon="md-camera" v-if="isCheck">审批补件</Button>
                </div>
            </Row>
            <br/>
            <Panel name="1">
                资产变更
                <div slot="content" >
                    <Row >
                    <Table border  :columns="changeAssets" :data="changeList"  >
                        <template slot-scope="{ row, index }" slot="keyName">
                            <span v-if="row.keyName=='brandName'">品牌</span>
                            <span v-if="row.keyName=='modelName'">车型</span>
                            <span v-if="row.keyName=='styleName'">款式</span>
                            <span v-if="row.keyName=='financeItemName'">金融产品</span>
                            <span v-if="row.keyName=='downPayScale'">车款首付比例</span>
                            <span v-if="row.keyName=='addDownPayScale'">附加贷首付比例</span>
                            <span v-if="row.keyName=='loanTerm'">车款期限</span>
                            <span v-if="row.keyName=='addLoanTerm'">附加贷期限</span>
                            <span v-if="row.keyName=='loanAmt'">车款贷款金额</span>
                            <span v-if="row.keyName=='addAmt'">附加贷贷款金额</span>
                            <span v-if="row.keyName=='contractAmt'">合同价格</span>
                            <span v-if="row.keyName=='addLoanName'">附加贷</span>
                        </template>
                        <template slot-scope="{ row, index }" slot="afterParam">
                            <span :class="row.beforParam !== row.afterParam ? 'red' :''">{{row.afterParam}}</span>
                        </template>
                    </Table>
                    </Row>
                </div>
            </Panel>
            <Panel name="2">
                留言
                <div slot="content" >
                    <Row >
                        <Table border  :columns="remindColums" :data="remindList" >
                            <template slot-scope="{ row, index }" slot="action">
                                <a v-if="row.cancel" @click="cancelRemind(row)">撤销</a>
                            </template>
                            <template slot-scope="{ row, index }" slot="remindType">
                                <span>{{setDictTitleByType("msgType",row.remindType)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="remindContent">
                                <span>{{setDictTitleByType("msgType",row.remindContent)}}</span>
                            </template>
                        </Table>
                    </Row>
                </div>
            </Panel>
            <Panel class="creditCardBox" name="3">
                信贷报告
                <div slot="content">
                    <!--<Row class="recordHistory" v-for="record in recordHisData">
                        <Icon type="ios-contact" class="icon" :size="70"/>
                        <Card :bordered="false" class="recordHistoryMsg" :style="record.style">
                            <h3 slot="title">{{record.title}}</h3>
                            <p>{{record.content}}</p>
                        </Card>
                    </Row>-->
                    <Row class="upApprove" v-for="record in upApprove" v-if="isCheck || holiday">
                        <Icon type="ios-contact" class="icon" :size="70"/>
                        <Card :bordered="false" style="width: 1000px">
                            <h3 slot="title">{{record.disposeStaff + record.approveSuggestName + record.approveEndTime}}</h3>
                            <p>{{record.approveRemark}}</p>
                        </Card>
                    </Row>
                    <Row >
                        <Form :model="suggestData" :label-width="100" class="suggestForm" ref="suggestForm" v-if="isCheck">
                            <FormItem label="审批建议" prop="approveSuggest" class="suggestSelect">
                                <i-select class="formSelect" v-model="suggestData.approveSuggest" @on-select="suggestSelected" @on-change="getSuggestReasonDic(true)" >
                                    <i-option v-for="item in approveSuggestDic" :value="item.value">{{item.title}}</i-option>
                                </i-select>
                            </FormItem>
                            <FormItem prop="approveMessage"  ref="remarkItem" v-show="remarkVisible"
                                      class="displayItem" style="margin-top: -26px;">
                                <Input type="textarea" v-model="suggestData.approveMessage"
                                       :rows="4"
                                       class="msgTextarea"
                                       :placeholder="'请输入'+formRemarkTitle" maxlength="512"/>
                            </FormItem>
                            <FormItem prop="approveRemark" >
                                <Input type="textarea" v-model="suggestData.approveRemark"
                                       :rows="6"
                                       class="remarkTextarea"
                                       @on-change="setRemindRemark"
                                        style="width: 768px"
                                       placeholder="请输入信贷分析报告" maxlength="512" />
                                <Row style="margin-left: 10px">
                                    <Button class="tailButton normalButton"  size="small" @click="saveApprove">暂存</Button>
                                    <Button class="tailButton normalButton" size="small" @click="submitApprove">提交</Button>
                                </Row>
                            </FormItem>
                            <Spin fix size="large" v-if="submitApproveLoading"></Spin>
                        </Form>
                    </Row>
                </div>
            </Panel>
        </Collapse>
        <Modal v-model="showUpload" :width="1100">
            <fileOperation  :uploadParam="uploadParam"  :isInt="canUpload" :path="path" ></fileOperation>
        </Modal>
    </div>
</template>

<script>
    import {
        setStatusIsOpen
    } from "@/projects/afs-core-business/api/afs-case/approve/approveTask";
    import {
        queryRemindListByApplyNo,
        queryCustSummaryInfoApplyNo,
        cancelRemindtById,
        queryRecordListByApplyNo
    } from "@/projects/afs-core-business/api/afs-case/approve/loanAnaylyze";
    import Remind from "../remind/remind";
    import {
        getByTypes
    } from "_p/basic/api/admin/datadic";
    import {getDictDataByType} from "../../../../../basic/api/admin/datadic";
    import {queryApprove,saveApprove,submitApprove,listHistoryContent,getChangeAssets,queryUpApprove,queryReturnApprove} from "../../../../api/afs-case/approve/changeAssetsTask";
    import fileOperation from "_p/basic/pages/image/upload-file/fileOperation.vue";
    export default {
        name: "loan-analyze",
        props: {
            applyNo: {
                required: false,
                default:()=>{
                    return '';
                }
            },
            stageId:{
                type:[String,Number],
                default:''
            }
        },
        components: {
            Remind,
            fileOperation

        },
        data:function () {
            return {
                detailParams:{},
                applyNumber:'',
                values:["1","2","3"],
                recording:false,
                auditParam:{
                    busiNo:'',
                    belongNo:'',
                    busiNode:"assetsChange"},
                dataDic:{},
                dicKeys: [
                    "msgType"
                ],
                remindColums:[
                    {title: "操作",key: "action",minWidth: 100,align: "center",slot:"action"},
                    {title: "留言类型",key: "remindType",minWidth: 100,align: "center",slot:"remindType"},
                    {title: "处理人",key: "disposeUser",minWidth: 100,align: "center"},
                    {title: "处理时间",key: "disposeTime",minWidth: 100,align: "center"},
                    {title: "处理意见",key: "remindContent",minWidth: 100,align: "left"}
                ],
                changeAssets:[
                    {title: "变量名称",key: "keyName",minWidth: 30,align: "left" ,slot:"keyName"},
                    {title: "变更前",key: "beforParam",minWidth: 200,align: "left"},
                    {title: "变更后",key: "afterParam",minWidth: 200,align: "left",slot:"afterParam"}
                ],
                remindList:[],
                recordingList:[],
                suggestTemplateId:'',
                suggestTemplateData:[],
                recordHisData:[],
                upRecord:[],
                upApprove: [],
                suggestData:{},
                approveSuggestDicKey:'approveSuggest',
                approveSuggestDic:[],
                currentReasonDic:[],
                visitStatusDicKey:'visitStatus',
                visitStatusDic:[],
                formRemarkTitle:'',
                visitData:{},
                isTrue:true,
                isCheck: false,
                appear:false,
                visitLevelModalVisible:false,
                reasonVisible:false,
                remarkVisible:false,
                submitLoading:false,
                submitApproveLoading:false,
                reasonSelectMultiple:true,
                changeList:[],
                holiday: false,
                businessStateIn: "",
                businessStateInList: [
                    "approved",
                    "reject",
                    "repeal",
                    "conditionalApproval",
                    "cancelConditional"
                ],
                suggestConst: {
                    check: {
                        title: "建议核准",
                        val: "suggestCheck"
                    },
                    reject: {
                        title: "建议拒绝",
                        val:"suggestReject"
                    },
                    cancel: {
                        title: "撤销",
                        val:"suggestCancel"
                    },
                    rejectFinal: {
                        title: "拒绝",
                        val:"suggestRejectF"
                    },
                    checkFinal: {
                        title: "核准",
                        val:"suggestCheckF"
                    },
                    sendBack:{
                        title:"退回",
                        val:"sendBack"
                    }
                },
                remindVisible : false,
                remindApproveRecord:{
                    applyNo: '',
                    stageId: '',
                    approveRemark: '',
                    useScene:'changeAssets'
                },
                uploadParam: {
                    busiNo: "",
                    belongNo: "",
                    busiType: "",
                    busiData: {}
                },
                canUpload:false,
                path:"case",
                showUpload:false,
            }

        },
        methods:{
            //返回
            whenClose(){
                this.afs.afsEmit(this,"flushData");
            },

            setRemindRemark(){
                this.$emit('on-set-approve',this.suggestData);
            },
            showRecord(){
                this.loadRecordList();
                this.recording = true;
            },
            getDic(v){
                let dic = []
                switch(v) {
                    case "msgType":
                        dic = this.dataDic.msgType;
                        break;
                    default:
                }
                return dic;
            },
            setDictTitleByType(v1,v2){
                let dic = {}
                if(!this.getDic(v1)) return '';
                this.getDic(v1).forEach(column => {
                    if(column.value === v2){
                        dic = column;
                    }
                });
                return dic.title;
            },
            initDataDic(){
                let self = this;
                getByTypes(this.dicKeys).then(res => {
                    if (res.code === "0000" && res.data) {
                        self.dataDic = res.data;
                    }
                });
            },
            queryRemind(){
                let params = {
                    applyNo: this.detailParams.applyNo,
                    stageId: this.detailParams.stageId,
                    useScene:'changeAssets'
                }
                queryRemindListByApplyNo(params).then(res => {
                    if (res.code === "0000") {
                        this.remindList = res.data;
                    }
                });
            },
            //add by gjq 更新打开状态
            updateStatusIsOpen(){
                let params="";
                params={
                    approveStaff:this.detailParams.disposeCode,
                    stageId:this.detailParams.stageId,
                    processType:this.detailParams.processType,
                    applyNo:this.detailParams.applyNo
                }
                setStatusIsOpen(params).then(res => {
                    if (res.code === "0000") {
                    }
                });
            },
            init() {
                this.initDataDic();
                this.queryRemind();
                this.getSuggestDic();
                this.queryApprove();
                /*this.initRecordHistory();*/
                this.getChangeAssetsData();
                this.queryUpApprove();
                this.queryReturnApprove();
                this.initCheckBusinessStateInList();
            },
            initCheckBusinessStateInList() {
                this.businessStateInList.forEach(list =>{
                    if (list === this.businessStateIn) {
                        this.holiday = true
                    }
                })
            },
            loadRecordList() {
                let params = {
                    applyNo: this.detailParams.applyNo
                }
                queryRecordListByApplyNo(params).then(res => {
                    if (res.code === "0000") {
                        this.recordingList = res.data;
                    }
                });
            },
            afterRadioChange(val){
            },
            clearSelectTemplate(){
                this.suggestData.approveRemark = '';
                this.setRemindRemark();
            },
            templateChange(val){
                this.suggestData.approveRemark = val;
                this.setRemindRemark();
            },
            changeAfterParam(val){
              if(!val.beforParam == val.afterParam) {

              }
            },
            getSuggestDic() {
                getDictDataByType(this.approveSuggestDicKey).then(res => {
                    if (res.code == '0000') {
                        // 默认复审
                        let suggestKey = [
                            this.suggestConst.check,
                            this.suggestConst.reject,
                            this.suggestConst.cancel
                        ];
                        // 只有一个审核节点，即是初审，也是终审
                        if(this.detailParams.firstTrail && this.detailParams.lastTrail){
                            suggestKey = [
                                this.suggestConst.checkFinal,
                                this.suggestConst.rejectFinal,
                                this.suggestConst.cancel
                            ]
                            // 终审
                        }else if(this.detailParams.lastTrail){
                            suggestKey = [
                                this.suggestConst.checkFinal,
                                this.suggestConst.rejectFinal,
                                this.suggestConst.cancel
                            ]
                            // 初审
                        }else if(this.detailParams.firstTrail){
                            suggestKey = [
                                this.suggestConst.check,
                                this.suggestConst.reject,
                                this.suggestConst.cancel
                            ]
                        }
                        suggestKey.forEach(suggestConst=>{
                            res.data.forEach( suggest =>{
                                if(suggestConst.val == suggest.value){
                                    this.approveSuggestDic.push(suggest);
                                }
                            })
                        })
                    }
                })
            },
            suggestSelected(val){
                this.suggestData.approveSuggestName = val.label;
            },
            getSuggestReasonDic(clear) {
                if(this.suggestData && this.suggestData.approveSuggest) {
                    getDictDataByType(this.suggestData.approveSuggest).then(res => {
                        if (res.code == "0000") {
                            this.currentReasonDic = res.data || [];
                        }
                    });
                    if (clear) {
                        if(this.suggestData) {
                            this.suggestData.approveReason = "";
                            this.suggestData.approveMessage = "";
                        }
                    }
                    // 审批原因
                    if (this.suggestData.approveSuggest == this.suggestConst.check.val
                        || this.suggestData.approveSuggest == this.suggestConst.checkFinal.val
                        || this.suggestData.approveSuggest == this.suggestConst.cancel.val
                        || this.suggestData.approveSuggest == this.suggestConst.reject.val) {
                        this.reasonVisible = false;
                    } else {
                        this.reasonVisible = true;
                    }
                    this.remarkVisible = false;
                    this.reasonSelectMultiple = true;
                    if(this.suggestData.approveSuggest == this.suggestConst.rejectFinal.val){
                        this.reasonSelectMultiple = false;
                    }
                }
            },
            /*//获取历史信贷报告
            initRecordHistory(){
                let params = {
                    applyNo: this.detailParams.applyNo,
                    check: false
                };
                    listHistoryContent(params).then(res => {
                        if (res.code == '0000') {
                            this.recordHisData = res.data.taskHistories || [];
                            this.recordHisData.forEach(record => {
                                if (record.approveSuggest != this.suggestConst.reformist.val) {
                                    let color;
                                    if (record.approveSuggest == this.suggestConst.condition.val
                                        || record.approveSuggest == this.suggestConst.conditionFinal.val) {
                                        color = "#00FFFF";
                                    } else if (record.approveSuggest == this.suggestConst.reject.val
                                        || record.approveSuggest == this.suggestConst.rejectFinal.val) {
                                        color = "#EE8D8F";
                                    } else if (record.approveSuggest == this.suggestConst.check.val
                                        || record.approveSuggest == this.suggestConst.checkFinal.val) {
                                        color = "#86F3AF";
                                    }
                                    record.style = {
                                        backgroundColor: color
                                    };
                                }
                            });
                        }
                    })
            },*/
            //获取上一岗审批记录
            queryUpApprove() {
              if (!this.detailParams.firstTrail) {
                  let params = {
                      applyNo : this.detailParams.applyNo,
                      stageId : this.detailParams.stageId
                  }
                  queryUpApprove(params).then(res=>{
                      if (res.code === "0000") {
                          this.upRecord = res.data || [];
                            this.upRecord.forEach(record => {
                                if (record.approveSuggest !== "sendBack" && record.approveSuggest !== "suggestReformist") {
                                    this.upApprove.push(record)
                                }
                            })
                      }
                  });
              }
            },
            //上一岗退回后，展示原审批意见
            queryReturnApprove() {
                let params = {
                    applyNo : this.detailParams.applyNo,
                    stageId : this.detailParams.stageId
                }
                queryReturnApprove(params).then(res =>{
                    if (res.code === "0000") {
                        if (res.data.approveSuggest !== undefined) {
                            if (res.data.approveSuggest !== "sendBack" && res.data.approveSuggest !== "suggestReformist") {
                                this.suggestData = res.data || {};
                            }
                        }
                    }
                })
            },
            //获取资产变更信息
            getChangeAssetsData(){
                    getChangeAssets(this.detailParams.applyNo).then(res => {
                        if (res.code == '0000') {
                                this.changeList = res.data;
                        }
                    })
            },
            saveApprove(){
                this.suggestData.applyNo= this.detailParams.applyNo;
                this.suggestData.stageId= this.detailParams.stageId;
                let params = {
                    approveRecord: this.suggestData
                };
                this.submitApproveLoading = true;
                saveApprove(params).then(res=>{
                    if(res.code == "0000"){
                        this.$Message.info("保存成功!")
                        this.suggestData = res.data.approveRecord;
                    }
                }).finally(()=>{
                    this.submitApproveLoading = false;
                })
            },
            submitApprove(){
                if(!this.suggestData.approveSuggest){
                    this.$Message.warning("请选择审批建议");
                    return ;
                }
                if(this.suggestData.approveSuggest == this.suggestConst.cancel.val
                    && (!this.suggestData.approveRemark || !this.suggestData.approveRemark.trim())){
                    this.$Message.warning("撤销备注不可为空");
                    return false;
                }
                if (this.detailParams.lastTrail) {
                    //一岗审批，信贷报告必填
                    if (this.detailParams.firstTrail) {
                        if (!this.suggestData.approveRemark) {
                            this.$Message.warning("请填写信贷分析报告")
                            return ;
                        }
                    }
                }
                //多岗审批只要不是最后一岗，信贷报告都必填
                if (!this.detailParams.lastTrail) {
                    if (!this.suggestData.approveRemark) {
                        this.$Message.warning("请填写信贷分析报告")
                        return ;
                    }
                }

                if(this.suggestData.approveSuggest != this.suggestConst.cancel.val) {
                    if (this.upRecord.length > 0 && this.upRecord[0].approveSuggest != this.suggestConst.sendBack.val
                      ) {
                        let lastApproveSuggest = this.upRecord[0].approveSuggest;
                        let currentApproveSuggest = this.suggestData.approveSuggest;
                        //只要不是首节点，就进行审批校验
                        if (!this.detailParams.firstTrail) {
                            if (this.detailParams.lastTrail) {
                                if ((lastApproveSuggest == this.suggestConst.check.val
                                    && currentApproveSuggest != this.suggestConst.checkFinal.val)
                                    || (lastApproveSuggest == this.suggestConst.reject.val
                                        && currentApproveSuggest != this.suggestConst.rejectFinal.val)) {
                                    this.$Message.warning("审批建议必须与上一岗位审核意见保持一致.");
                                    return false;
                                }
                            }else if (lastApproveSuggest != currentApproveSuggest) {
                                this.$Message.warning("审批建议必须与上一岗位审核意见保持一致.");
                                return false;
                            }
                        }
                    }
                }
                this.$Modal.confirm({
                   title:"确认",
                   content:"确认提交?",
                   onOk:()=>{
                       this.suggestData.stageId= this.detailParams.stageId;
                       this.suggestData.applyNo= this.detailParams.applyNo;
                       let params = {
                           approveRecord: this.suggestData
                       };
                       this.submitApproveLoading = true;
                       submitApprove(params).then(res=>{
                           if(res.code == "0000"){
                               this.$emit("whenClose");
                               this.$Message.info("提交成功!");
                               this.whenClose();
                               window.close();
                           }
                       }).finally(()=>{
                           this.submitApproveLoading = false;
                       })
                   }
                });
            },
            queryApprove(){
                let params = {
                    applyNo:this.detailParams.applyNo,
                    stageId:this.detailParams.stageId
                };
                queryApprove(params).then(res=>{
                    if(res.code == "0000"){
                        this.suggestData = res.data || {};
                        if(res.data) {
                            this.getSuggestReasonDic(false);
                        }
                    }
                });
            },
            closeThis() {
                this.whenClose();
                window.close();
            },
            //留言
            modifyRemind(){
                this.remindApproveRecord.applyNo = this.detailParams.applyNo;
                this.remindApproveRecord.stageId = this.detailParams.stageId;
                this.remindVisible=true;
            },
            addPaper() {
                this.uploadParam.busiNo=this.detailParams.applyNo;
                this.uploadParam.belongNo=this.detailParams.applyNo;
                this.uploadParam.busiType="creditApprove";
                this.showUpload=true;
                this.canUpload=true;

            },
            //撤销
            cancelRemind(v) {
                this.$Modal.confirm({
                    title: "撤销留言",
                    content: "确认撤销当前留言?",
                    onOk: () => {
                        let params = {
                            remindId: v.id
                        }
                        cancelRemindtById(params).then(res => {
                            if (res.code === "0000") {
                                if (res.msg === "success") {
                                    params = {
                                        applyNo: this.detailParams.applyNo,
                                        useScene:'changeAssets'
                                    }
                                    queryRemindListByApplyNo(params).then(result => {
                                        if (result.code === "0000") {
                                            this.remindList = result.data;
                                        }
                                    });
                                    this.$Message['success']({
                                        background: true,
                                        content: '留言撤销成功'
                                    });
                                } else {
                                    this.$Message['error']({
                                        background: true,
                                        content: '留言撤销失败'
                                    });
                                }
                            }
                        });
                    }
                })
            },
            afterRemindSubmit(){
                    this.queryRemind();
            },
        },
        mounted() {
            this.init();
        },
        created() {
            this.detailParams.applyNo = this.applyNo;
            this.detailParams.stageId = this.stageId;
            this.detailParams = this.afs.getPageParams(this);
            this.isCheck = this.detailParams.isCheck === "true";
            this.appear = this.detailParams.appear === "true";
            this.auditParam.busiNo=this.detailParams.applyNo;
            this.auditParam.belongNo=this.detailParams.applyNo;
            this.businessStateIn = this.detailParams.businessStatusIn;
            if(this.detailParams.isCheck === "true"){
                this.updateStatusIsOpen();
            }
        },
        computed:{
        }

    }
</script>

<style scoped>
.line>>>a {
  color: white;
}
</style>
